## Copyright (C) 2020 deesix <deesix@tuta.io>
## Copyright (C) 2020 Sanne Wouda
## This file is part of M2-Planet.
##
## M2-Planet is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## M2-Planet is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with M2-Planet.  If not, see <http://www.gnu.org/licenses/>.

DEFINE NULL 0000000000000000

# Stack (x18 as SP, 64 bits per element)
DEFINE STR____X0,[X18,-8]! 408e1ff8
DEFINE STR____X1,[X18,-8]! 418e1ff8
DEFINE STR____X16,[X18,-8]! 508e1ff8
DEFINE STR____X17,[X18,-8]! 518e1ff8
DEFINE STR____X30,[X18,-8]! 5e8e1ff8

DEFINE LDR____X0,[X18],8 408640f8
DEFINE LDR____X1,[X18],8 418640f8
DEFINE LDR____X16,[X18],8 508640f8
DEFINE LDR____X17,[X18],8 518640f8
DEFINE LDR____X30,[X18],8 5e8640f8

DEFINE MOV____X18,SP f2030091 # mov x18, sp


# Jump/branch/call/return
DEFINE BR_____X16 00021fd6
DEFINE BLR____X16 00023fd6
DEFINE RET c0035fd6

DEFINE CBZ____X0,20 a00000b4
DEFINE CBNZ___X0,20 a00000b5

DEFINE B.EQ___8 40000054
DEFINE B.NE___8 41000054
DEFINE B.LT___8 4b000054
DEFINE B.LE___8 4d000054
DEFINE B.GT___8 4c000054
DEFINE B.GE___8 4a000054

DEFINE B.CC___8 43000054
DEFINE B.LS___8 49000054
DEFINE B.CS___8 42000054
DEFINE B.HI___8 48000054

DEFINE B______8 02000014


# Load literals (PC-relative)
DEFINE LDRSW__X0,8 40000098
DEFINE LDR____W1,8 41000018
DEFINE LDR____W2,8 42000018
DEFINE LDR____W16,8 50000018


# Load/store/dereference
DEFINE LDR____X0,[X18] 400240f9
DEFINE STR____X0,[X1] 200000f9
DEFINE STRB___W0,[X1] 20000039
DEFINE LDR____X0,[X0] 000040f9
DEFINE LDRB___W0,[X0] 00004039


# Move data between registers
DEFINE MOV____X0,X17 e00311aa
DEFINE MOV____X1,X0 e10300aa
DEFINE MOV____X1,X18 e10312aa
DEFINE MOV____X2,X0 e20300aa
DEFINE MOV____X3,X0 e30300aa
DEFINE MOV____X4,X0 e40300aa
DEFINE MOV____X5,X0 e50300aa
DEFINE MOV____X6,X0 e60300aa
DEFINE MOV____X16,X0 f00300aa
DEFINE MOV____X16,X18 f00312aa
DEFINE MOV____X17,X16 f10310aa
DEFINE MOV____X17,X18 f10312aa


# Move constant to register
DEFINE MOV____X0,0 000080d2
DEFINE MOV____X0,1 200080d2
DEFINE MOV____X0,17 200280d2
DEFINE MOV____X0,-1 00008092
DEFINE MOV____W0,-1 00008012
DEFINE MOV____X1,0 010080d2
DEFINE MOV____X1,2 410080d2
DEFINE MOV____X1,8 010180d2
DEFINE MOV____X2,1 220080d2
DEFINE MOV____X0,-100 600c8092


# Arith/logic/relational
DEFINE ADD____X0,X1,X0 2000008b
DEFINE ADD____X0,X17,X0 2002008b
DEFINE ADD____X1,X18,8 41220091

DEFINE SUB____X0,X1,X0 200000cb
DEFINE SUB____X0,X0,X1 000001cb
DEFINE SUB____X0,X0,8 002000d1
DEFINE SUB____X0,X0,16 004000d1
DEFINE SUB____X0,X0,24 006000d1
DEFINE MSUB___X0,X0,X2,X1 0084029b

DEFINE MUL____X0,X1,X0 207c009b
DEFINE SDIV___X0,X1,X0 200cc09a
DEFINE SDIV___X2,X1,X0 220cc09a
DEFINE UDIV___X0,X1,X0 2008c09a
DEFINE UDIV___X2,X1,X0 2208c09a

DEFINE LSL____X0,X0,X2 0020c29a
DEFINE LSL____X0,X1,X0 2020c09a
DEFINE LSR____X0,X1,X0 2024c09a
DEFINE ASR____X0,X1,X0 2028c09a

DEFINE MVN____X0,X0 e00320aa
DEFINE AND____X0,X1,X0 2000008a
DEFINE ORR____X0,X1,X0 200000aa
DEFINE EOR____X0,X0,X1 000001ca

DEFINE CMP____X1,X0 3f0000eb


# Syscall
DEFINE MOV____X8,214 c81a80d2
DEFINE MOV____X8,49 280680d2
DEFINE MOV____X8,220 881b80d2
DEFINE MOV____X8,57 280780d2
DEFINE MOV____X8,221 a81b80d2
DEFINE MOV____X8,93 a80b80d2
DEFINE MOV____X8,48 080680d2
DEFINE MOV____X8,50 480680d2
DEFINE MOV____X8,53 a80680d2
DEFINE MOV____X8,17 280280d2
DEFINE MOV____X8,62 c80780d2
DEFINE MOV____X8,56 080780d2
DEFINE MOV____X8,63 e80780d2
DEFINE MOV____X8,160 081480d2
DEFINE MOV____X8,260 882080d2
DEFINE MOV____X8,64 080880d2
DEFINE SVC____0 010000d4
